import app from "../main";

// 对于state的操作
export default {

    /**
     * 改变左侧菜单的宽度
     * @param state 全局状态
     * @param width 改变后的宽度
     */
    changeSidebarWidth(state, width){
        state.sidebarWidth = width;
    },

    /**
     * 是否展开状态取反
     * @param state 状态
     */
    triggerCollapse(state){
        state.collapse = !state.collapse;
    },

    /**
     * 改变全屏状态
     * @param state 状态
     */
    changeFullScreen(state) {
        state.fullscreen = !state.fullscreen;
    },

    /**
     * 改变全屏状态
     * @param state 状态
     */
    reloadMainPage(state) {
        state.mainPageAlive = false;
        app.$nextTick(() => {
            state.mainPageAlive = true;
        });
    },

    /**
     * 改变当前拖动下标
     * @param state 状态
     * @param index 新下标
     */
    changeCurrentIndex(state, index) {
        state.currentIndex = index;
    },

    /**
     * 表单构建设置
     * @param state 状态
     * @param data 新设置
     */
    changeFormControlsSettingData(state, data) {
        state.formControlsSettingData = data;
    },

    /**
     * 全屏
     */
    launchFullscreen() {
        let element = document.documentElement;
        if(element.requestFullscreen) {
            element.requestFullscreen()
        } else if(element.mozRequestFullScreen) {
            element.mozRequestFullScreen()
        } else if(element.msRequestFullscreen) {
            element.msRequestFullscreen()
        } else if(element.webkitRequestFullscreen) {
            element.webkitRequestFullScreen()
        }
    },
    /**
     * 退出全屏
     */
    exitFullscreen() {
        if(document.exitFullscreen) {
            document.exitFullscreen()
        } else if(document.mozCancelFullScreen) {
            document.mozCancelFullScreen()
        } else if(document.msExitFullscreen) {
            document.msExiFullscreen()
        } else if(document.webkitCancelFullScreen) {
            document.webkitCancelFullScreen()
        }
    },

    /**
     * 暴露出去的方法
     */
    triggerFullScreen() {
        // 注意：要在用户授权全屏后才能获取全屏的元素，否则 fullscreenEle为null
        let fullscreenEle = document.fullscreenElement || document.mozFullScreenElement || document.webkitFullscreenElement;
        // console.log(fullscreenEle, '全屏元素...')
        if(fullscreenEle) {
            // console.log('exitFullscreen..')
            this.commit('exitFullscreen')
        } else {
            // console.log('lanchFullscreen..')
            this.commit('launchFullscreen')
        }
    }
};
